/**
 * gwt-validator 
 *
 * Copyright (C) 2006  Nikolaus Rumm, Christian Simonutti
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package gwt.validator.client;

import gwt.validator.client.action.Action;
import gwt.validator.client.action.AddMessageAction;
import gwt.validator.client.action.ConditionalInvokerAction;
import gwt.validator.client.action.FirstInvocationCondition;
import gwt.validator.client.action.SetFocusAction;
import gwt.validator.client.action.SetMessageAction;
import gwt.validator.client.check.Check;
import gwt.validator.client.check.DatePatternCheck;
import gwt.validator.client.check.EmailAddressCheck;
import gwt.validator.client.check.MandatoryCheck;
import gwt.validator.client.check.StringLengthRangeCheck;
import gwt.validator.client.check.StringMaxLengthCheck;
import gwt.validator.client.check.StringMinLengthCheck;
import gwt.validator.client.check.StringPatternCheck;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Panel for testing the String validators.
 * 
 * @author <a href="mailto:nikolaus.rumm@gmail.com">Nikolaus Rumm</a>
 */
public class StringTestPanel extends VerticalPanel {

	private Label mandatoryValidatorLabel;
	private TextBox mandatoryValidatorText;
	private Label mandatoryValidatorErrorLabel;

	private Label minLengthValidatorLabel;
	private TextBox minLengthValidatorText;
	private Label minLengthValidatorErrorLabel;

	private Label maxLengthValidatorLabel;
	private TextBox maxLengthValidatorText;
	private Label maxLengthValidatorErrorLabel;

	private Label rangeValidatorLabel;
	private TextBox rangeValidatorText;
	private Label rangeValidatorErrorLabel;

	private Label patternValidatorLabel;
	private TextBox patternValidatorText;
	private Label patternValidatorErrorLabel;

	private Label emailPatternValidatorLabel;
	private TextBox emailPatternValidatorText;
	private Label emailPatternValidatorErrorLabel;

	private Label dateValidatorLabel;
	private TextBox dateValidatorText;
	private Label dateValidatorErrorLabel;

	private Label errorLabel;

	/**
	 * Constructor
	 */
	public StringTestPanel() {
		this.init();
	}

	/**
	 * Initializes the panel's widgets
	 */
	public void init() {
		this.mandatoryValidatorLabel = new Label("Mandatory");
		this.mandatoryValidatorText = new TextBox();
		this.mandatoryValidatorErrorLabel = new Label();
		this.mandatoryValidatorErrorLabel.setStyleName("error");

		this.rangeValidatorLabel = new Label("Range validator");
		this.rangeValidatorText = new TextBox();
		this.rangeValidatorErrorLabel = new Label();
		this.rangeValidatorErrorLabel.setStyleName("error");

		this.minLengthValidatorLabel = new Label("Min length validator");
		this.minLengthValidatorText = new TextBox();
		this.minLengthValidatorErrorLabel = new Label();
		this.minLengthValidatorErrorLabel.setStyleName("error");

		this.maxLengthValidatorLabel = new Label("Max length validator");
		this.maxLengthValidatorText = new TextBox();
		this.maxLengthValidatorErrorLabel = new Label();
		this.maxLengthValidatorErrorLabel.setStyleName("error");

		this.patternValidatorLabel = new Label("Pattern validator");
		this.patternValidatorText = new TextBox();
		this.patternValidatorErrorLabel = new Label();
		this.patternValidatorErrorLabel.setStyleName("error");

		this.emailPatternValidatorLabel = new Label("Email validator");
		this.emailPatternValidatorText = new TextBox();
		this.emailPatternValidatorErrorLabel = new Label();
		this.emailPatternValidatorErrorLabel.setStyleName("error");

		this.dateValidatorLabel = new Label("Date validator");
		this.dateValidatorText = new TextBox();
		this.dateValidatorErrorLabel = new Label();
		this.dateValidatorErrorLabel.setStyleName("error");

		Grid g = new Grid(7, 3);
		g.setWidget(0, 0, this.mandatoryValidatorLabel);
		g.setWidget(0, 1, this.mandatoryValidatorText);
		g.setWidget(0, 2, this.mandatoryValidatorErrorLabel);
		g.setWidget(1, 0, this.rangeValidatorLabel);
		g.setWidget(1, 1, this.rangeValidatorText);
		g.setWidget(1, 2, this.rangeValidatorErrorLabel);
		g.setWidget(2, 0, this.minLengthValidatorLabel);
		g.setWidget(2, 1, this.minLengthValidatorText);
		g.setWidget(2, 2, this.minLengthValidatorErrorLabel);
		g.setWidget(3, 0, this.maxLengthValidatorLabel);
		g.setWidget(3, 1, this.maxLengthValidatorText);
		g.setWidget(3, 2, this.maxLengthValidatorErrorLabel);
		g.setWidget(4, 0, this.patternValidatorLabel);
		g.setWidget(4, 1, this.patternValidatorText);
		g.setWidget(4, 2, this.patternValidatorErrorLabel);
		g.setWidget(5, 0, this.emailPatternValidatorLabel);
		g.setWidget(5, 1, this.emailPatternValidatorText);
		g.setWidget(5, 2, this.emailPatternValidatorErrorLabel);
		g.setWidget(6, 0, this.dateValidatorLabel);
		g.setWidget(6, 1, this.dateValidatorText);
		g.setWidget(6, 2, this.dateValidatorErrorLabel);

		this.add(g);

		Button b = new Button("Validate");
		b.addClickListener(new ClickListener() {

			public void onClick(Widget aSender) {
				validate();
			}

		});
		this.add(b);

		this.errorLabel = new Label();
		this.errorLabel.setStyleName("error");
		this.add(errorLabel);
	}

	public void validate() {
		this.errorLabel.setText("");

		// this is more or less a mutable Boolean...
		FirstInvocationCondition scopeCondition = new FirstInvocationCondition();

		new Validator(
				new ValidationScope[] {
						new ValidationScope(new Check[] { new MandatoryCheck(
								this.mandatoryValidatorText,
								this.mandatoryValidatorLabel) }, new Action[] {
								new SetMessageAction(
										this.mandatoryValidatorErrorLabel),
								new AddMessageAction(this.errorLabel),
								new ConditionalInvokerAction(
										new SetFocusAction(
												this.mandatoryValidatorText),
										scopeCondition) }),
						new ValidationScope(
								new Check[] { new StringLengthRangeCheck(
										this.rangeValidatorText,
										this.rangeValidatorLabel, 5, 10) },
								new Action[] {
										new SetMessageAction(
												this.rangeValidatorErrorLabel),
										new AddMessageAction(this.errorLabel),
										new ConditionalInvokerAction(
												new SetFocusAction(
														this.rangeValidatorText),
												scopeCondition) }),
						new ValidationScope(
								new Check[] { new StringMinLengthCheck(
										this.minLengthValidatorText,
										this.minLengthValidatorLabel, 3) },
								new Action[] {
										new SetMessageAction(
												this.minLengthValidatorErrorLabel),
										new AddMessageAction(this.errorLabel),
										new ConditionalInvokerAction(
												new SetFocusAction(
														this.minLengthValidatorText),
												scopeCondition) }),
						new ValidationScope(
								new Check[] { new StringMaxLengthCheck(
										this.maxLengthValidatorText,
										this.maxLengthValidatorLabel, 3) },
								new Action[] {
										new SetMessageAction(
												this.maxLengthValidatorErrorLabel),
										new AddMessageAction(this.errorLabel),
										new ConditionalInvokerAction(
												new SetFocusAction(
														this.maxLengthValidatorText),
												scopeCondition) }),
						new ValidationScope(
								new Check[] { new StringPatternCheck(
										this.patternValidatorText,
										this.patternValidatorLabel, "[a-z]*") },
								new Action[] {
										new SetMessageAction(
												this.patternValidatorErrorLabel),
										new AddMessageAction(this.errorLabel),
										new ConditionalInvokerAction(
												new SetFocusAction(
														this.patternValidatorText),
												scopeCondition) }),
						new ValidationScope(
								new Check[] { new EmailAddressCheck(
										this.emailPatternValidatorText,
										this.emailPatternValidatorLabel) },
								new Action[] {
										new SetMessageAction(
												this.emailPatternValidatorErrorLabel),
										new AddMessageAction(this.errorLabel),
										new ConditionalInvokerAction(
												new SetFocusAction(
														this.emailPatternValidatorText),
												scopeCondition) }),
						new ValidationScope(
								new Check[] { new DatePatternCheck(
										this.dateValidatorText,
										this.dateValidatorLabel, "yyyy-MM-dd") },
								new Action[] {
										new SetMessageAction(
												this.dateValidatorErrorLabel),
										new AddMessageAction(this.errorLabel),
										new ConditionalInvokerAction(
												new SetFocusAction(
														this.dateValidatorText),
												scopeCondition) }) })
				.validate();
	}
}
